package com.tronsis.distribution.widget.address;

import android.content.Context;

import com.tronsis.distribution.bean.RegionBean;

import java.util.ArrayList;
import java.util.List;


class DefaultAddressProvider implements AddressProvider {
   private RegionBean mRegionBean;
    private List<RegionBean> provinceList = new ArrayList<>();
    private List<RegionBean> cityList = new ArrayList<>();
    private List<RegionBean> countyList = new ArrayList<>();
    private List<RegionBean> streetList = new ArrayList<>();


    DefaultAddressProvider(Context context,RegionBean regionBean) {
     this.mRegionBean = regionBean;
    }

    @Override
    public void provideProvinces(final AddressReceiver<RegionBean> addressReceiver) {
//        List<RegionBean> list = new ArrayList<>();
//        list.add(mRegionBean);
//        addressReceiver.send(list);
        provinceList = mRegionBean.getChildren();
        addressReceiver.send(provinceList);
    }

    @Override
    public void provideCitiesWith(String provinceId, final AddressReceiver<RegionBean> addressReceiver) {
//        cityList = mRegionBean.getChildren();
//        addressReceiver.send(mRegionBean.getChildren());
        for (RegionBean regionBean : provinceList) {
            if (regionBean.getCode().equals(provinceId)) {
                cityList = regionBean.getChildren();
                break;
            }
        }
        addressReceiver.send(cityList);
    }

    @Override
    public void provideCountiesWith(String cityId, final AddressReceiver<RegionBean> addressReceiver) {
        for (RegionBean regionBean : cityList) {
            if (regionBean.getCode().equals(cityId)) {
                countyList = regionBean.getChildren();
                break;
            }
        }
        addressReceiver.send(countyList);
    }

    @Override
    public void provideStreetsWith(String countyId, final AddressReceiver<RegionBean> addressReceiver) {
        for (RegionBean regionBean : countyList) {
            if (regionBean.getCode().equals(countyId)) {
                streetList = regionBean.getChildren();
                break;
            }
        }
        addressReceiver.send(streetList);
    }

}
